package com.isczy.userservice.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.userservice.entity.UcenterMember;
import com.isczy.userservice.entity.vo.RegisterVo;
import com.isczy.userservice.service.UcenterMemberService;
import com.isczy.utils.DataResult;
import com.isczy.utils.JwtUtils;
import com.isczy.utils.ResultCode;
import com.isczy.utils.ordervo.UcenterMemberOrder;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author CZY
 * @since 2021-01-18
 */
@Api(description = "用户会员表 前端控制器")
@RestController
@RequestMapping("/userservice/ucenter-member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /***
     * ***************************************
     * 功能描述:登录
     * @param member
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-18 11:21】
     * @author modify:
     */
    @PostMapping("login")
    public DataResult loginUser(@RequestBody UcenterMember member) {
        //返回token值，使用jwt生成
        try{
            return memberService.login(member);
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据课程id进行章节查询失败")
                    .params(member)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:注册
     * @param registerVo
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-18 11:21】
     * @author modify:
     */
    @PostMapping("register")
    public DataResult registerUser(@RequestBody RegisterVo registerVo) {
        try{
            return memberService.register(registerVo);
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据课程id进行章节查询失败")
                    .params(registerVo)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据token获取用户信息
     * @param request
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-18 11:57】
     * @author modify:
     */
    @GetMapping("getMemberInfo")
    public DataResult getMemberInfo(HttpServletRequest request) {
        try{
            //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            //查询数据库根据用户id获取用户信息
            return DataResult.builder()
                    .data(memberService.getById(memberId))
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据课程id进行章节查询失败")
                    .params(request.getHeader("token"))
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据用户id获取用户信息
     * @param id
     * @return {@link UcenterMemberOrder}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-21 15:49】
     * @author modify:
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    /***
     * ***************************************
     * 功能描述:查询某一天注册人数
     * @param day
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-23 10:06】
     * @author modify:
     */
    @GetMapping("countRegister/{day}")
    public DataResult countRegister(@PathVariable String day) {
        try{
            return DataResult.builder()
                    .data(memberService.countRegisterDay(day))
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("查询某一天注册人数失败")
                    .params(day)
                    .build();
        }

    }
}

